"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Player = void 0;
const Room_1 = require("./Room");
const WebSocket_1 = require("./WebSocket");
class Player {
    constructor(id, connection) {
        this.roomId = -1; //房间号
        this.id = id;
        this.connection = connection;
        this.timer = setInterval(() => {
            this.heartbeat();
        }, 1000);
    }
    /**
     * 开启匹配
     */
    matching(obj) {
        //找房间加入
        for (let room of WebSocket_1.map_room.values()) {
            if (room.state == Room_1.RoomState.OPEN && room.type == obj.type && room.arr_players.length < room.type) {
                room.addPlayer(this);
                return;
            }
        }
        //没找到房间就自己创建一个
        const room = (0, WebSocket_1.createRoom)(obj.type);
        WebSocket_1.map_room.set(room.id, room);
        room.addPlayer(this);
    }
    /**
     * 取消匹配
     */
    cancel() {
        WebSocket_1.map_room.get(this.roomId).reducePlayer(this);
    }
    /**
     * 匹配成功
     */
    success() {
        WebSocket_1.arr_hallPlayer.delete(this);
        (0, WebSocket_1.numOfHallPlayers)();
    }
    /**
     * 玩家掉线
     */
    dropLine() {
        var _a;
        clearInterval(this.timer);
        if (this.roomId != -1) {
            (_a = WebSocket_1.map_room.get(this.roomId)) === null || _a === void 0 ? void 0 : _a.dropLine(this);
        }
    }
    /**
     * 检查版本号
     */
    checkVersion(version) {
        (0, WebSocket_1.sendObj)(this.connection, { other: "check_version", "result": version == WebSocket_1.VERSION });
    }
    /**
     * 心跳检测
     */
    heartbeat() {
        (0, WebSocket_1.sendObj)(this.connection, { other: "heartbeat" });
    }
}
exports.Player = Player;
